<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 15-3-24
 * Time: 上午10:42
 * 消费管理
 */
namespace Factory\Controller;
use Mcit\Page;

class BillController extends BaseController{

    public function index(){
        ini_set("max_execution_time", "1800");
        //2---厂商  3---运营商
        if(session("fac_type") == 2){
            if (session('fac_key')){
                $condition_str="b.ev_fac_code='".session('fac_key')."' and a.yy_user_id='".session('uid')."'";
            }else{
                $condition_str="b.ev_fac_code='".session('fac_login_name')."'";
            }
        }elseif(session("fac_type") == 3){
            if(session('pid') != 0){
                $operation = "a.plant_attr=3 and a.yy_user_id='".session('uid')."'";
            }else{
                $yyuserid = M()->query("select id from ev_fac where id='".session('uid')."' OR pid='".session('uid')."'");
                foreach($yyuserid as $k=>$v){
                    $yyuserids .= $v['id'].",";
                }
                $yyuserids = trim($yyuserids,',');
                $operation = "a.plant_attr=3 and a.yy_user_id IN($yyuserids)";
            }
        }
        $keylink       = I('key_link');
        $user_pay_card = I('user_pay_card');
        $strt_time     = I("start_time");
        $end_time      = date("Y-m-d H:s:i",strtotime(I("end_time")));
        $times         = I("times");
        //会员卡号
        if(!empty($_GET['user_pay_card'])){
            $maps      = "and c.user_pay_card like '$user_pay_card%'";
        }
        //设备地址
        if(!empty($_GET['key_link'])){
            $maps      = "and c.key_link like '$keylink%'";
        }
        //时间段
        if(!empty($_GET['end_time'])){
            $strt_time = date("Y-m-d H:s:i",strtotime(I("start_time")));
            $maps      = "and c.time >= '$strt_time' and c.time <= '$end_time'";
        }
        if(!empty($_GET['start_time']) && $_GET['end_time'] == ''){
            $maps      = "and c.time like '$strt_time%'";
        }
        //时间段+会员卡号
        if(!empty($_GET['end_time']) && !empty($_GET['user_pay_card'])){
            $strt_time = date("Y-m-d H:s:i",strtotime(I("start_time")));
            $maps      = "and c.user_pay_card like '$user_pay_card%' and c.time >= '$strt_time' and c.time <= '$end_time'";
        }
        if(!empty($_GET['start_time']) && $_GET['end_time'] == '' && !empty($_GET['user_pay_card'])){
            $maps      = "and c.user_pay_card like '$user_pay_card%' and c.time like '$strt_time%'";
        }
        //时间段+设备地址
        if(!empty($_GET['end_time']) && !empty($_GET['key_link'])){
            $strt_time = date("Y-m-d H:s:i",strtotime(I("start_time")));
            $maps      = "and c.key_link like '$keylink%' and c.time >= '$strt_time' and c.time <= '$end_time'";
        }
        if(!empty($_GET['start_time']) && $_GET['end_time'] == '' && !empty($_GET['key_link'])){
            $maps      = "and c.key_link like '$key_link%' and c.time like '$strt_time%'";
        }
        //指定日期+设备地址
        if(!empty($_GET['times']) && !empty($_GET['key_link'])){
            $maps      = "and c.key_link = '$keylink' and c.time like '$times%'";
        }
        $sql           = "select * from ev_plant a
            left join ev_char b on a.id=b.ev_plant_id
            left join char_pay_record c on c.key_link=b.key_link
            where {$condition_str} {$operation} and c.user_pay_card <> '' {$maps}";
        //分页
        $count = count(M()->query($sql));
        // 重搜一律回第一页
        if( $_GET['fsign'] == "abnormal" ){
            $_GET['fsign'] = "normal";
            $_GET['page']  = 1;
        }
        vendor('Mcit.Sys.Page');
        $Page = new Page($count, 20);
        $pageView = $Page->show();
        $this->assign('pageView', $pageView); //发送分页
        $nowPage = isset($_GET['page']) ? $_GET['page'] * 20 - 20 : 0;
        $sql1    = "select a.name,a.location,c.id,c.user_pay_card,c.start_time,c.end_time,c.this_reading,c.this_amount,b.key_link
            from ev_plant a
            left join ev_char b on a.id=b.ev_plant_id
            left join char_pay_record c on c.key_link=b.key_link
            where {$condition_str} {$operation} and c.user_pay_card <> '' {$maps}
            limit $nowPage,".$Page->listRows;
        $pay = M()->query($sql1);
//        echo M()->getLastSql();
        //分页结束
        foreach($pay as $k=>$v){
            if(mb_strlen($v['name'],'utf-8') <= 17){
                $pay[$k]['plant_name'] = $v['name'];
            }else{
                $pay[$k]['plant_name'] = mb_substr($v['name'],0,17,"utf-8")."···";
            }
            $region_arr = strsToArray($v['location']);//查所在地
            $region     = D("Region")->where("code='".$region_arr[2]."'")->getField('fullname');
            $pay[$k]['location_text'] = $region;
            $pay[$k]['plant_text']    = $v['name'];
            $pay[$k]['time_num']      = round((strtotime($v['end_time']) - strtotime($v['start_time'])) / 3600,2);
        }
//        P($pay);
        $this->assign('pay',$pay);
        $this->display();
    }

    //查看当前详细信息
    public function detail(){

        if(IS_GET){
            $id = I('id');
            $sql = "select a.key_link,a.user_pay_card,a.start_time,a.end_time,a.this_reading,a.start_em,a.end_em,
                    a.this_amount,a.offline_balance_start,a.offline_balance_end,c.name,c.location,c.address,d.fullname
                    from char_pay_record a inner join ev_char b on b.key_link=a.key_link inner join ev_plant c on c.id=b.ev_plant_id
                    inner join ev_region d on d.code=substring_index(c.location,',',-1) where a.id=".$id;
            $data = M()->query($sql);
            foreach($data as $v){
                $data = $v;
            }
            $this->data = $data;
        }
        $this->display();
    }
}
